package com.dobby.pea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dobby.pea.common.BaseContext;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.OrderDto;
import com.dobby.pea.entity.tbOrder;
import com.dobby.pea.entity.tbOrderDetail;
import com.dobby.pea.entity.tbShoppingCart;
import com.dobby.pea.service.OrderService;
import com.dobby.pea.service.ShoppingCartService;
import com.dobby.pea.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */

@RestController
@RequestMapping("/api/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取所有订单
     * @return
     */
    @GetMapping("/all")
    public R<List<tbOrder>> allorder(){

        LambdaQueryWrapper<tbOrder> queryWrapper = new LambdaQueryWrapper<>();

        List<tbOrder> list = orderService.list(queryWrapper);

        return R.success(list);

    }

    /**
     * 用户订单提交
     * @param ids
     * @return
     */
    @PostMapping("/submit")
    public R<tbOrder> submit(@RequestBody List<Long> ids){
        log.info("订单提交构建订单");

        tbOrder order=orderService.submit(ids);
        return R.success(order);

    }


    /**
     * 用户订单下单
     * @param order
     * @return
     */
    @PostMapping("/orders")
    public R<String> orders(@RequestBody tbOrder order){
        return orderService.orders(order);
    }

    /**
     * 返回用户该所有订单
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        log.info("返回用户该所有订单----->{},{}",page,pageSize);
        return orderService.orderDtoPage(page,pageSize,BaseContext.getCurrentId());
    }


    /**
     * 根据订单id 返回订单
     * @param id
     * @return
     */
    @GetMapping("/orderbyid/{id}")
    public R<OrderDto> OrderById(@PathVariable Long id){
        log.info("根据订单id 返回订单-------{}",id);
        OrderDto orderDto = orderService.OrderById(id);
        return R.success(orderDto);

    }

    /**
     *     根据商品id返回该商品所有的订单细节
     * @param id
     * @return
     */
    @GetMapping("/OrderDetailById/{id}")
    public R<List<OrderDto>> OrderDetailByGoodId(@PathVariable Long id){
        log.info("根据商品id返回该商品所有的订单细节-------{}",id);
        return orderService.orderByGood(id);
    }

    /**
     * 根据根据订单细节id修改订单细节
     * @param tbOrderDetail
     * @return
     */
    @PutMapping("/OrderDetailUpdate/{id}")
    public R<String> updateOrderDetail(@RequestBody tbOrderDetail tbOrderDetail){
        log.info("根据根据订单细节id修改订单细节-------{}",tbOrderDetail);

        String s = orderService.updateOrderDetail(tbOrderDetail);
        return R.success(s);
    }





}
